/*
 * Facet.java
 *
 * Created on 26 marzo 2004, 15.28
 */

package net.ermannofranco.xml.schema;

/**
 * Schema facets, mainly used in restriction.
 * 
 * @author ermanno (ermanno.franco@gmail.com)
 */
public enum Facet {

	MAXEXCLUSIVE, MINEXCLUSIVE, MAXINCLUSIVE, MININCLUSIVE, TOTALDIGITS, FRACTIONDIGITS, LENGTH, MINLENGTH, MAXLENGTH, ENUMERATION, WHITESPACE, PATTERN;

	public interface Whitespace {
		/**
		 * Costanti per whitespace.
		 */
		String COLLAPSE = "collapse";
		String PRESERVE = "preserve";
		String REPLACE = "replace";
	}

	protected static String getName(Facet facetType) {
		switch (facetType) {
		case MAXEXCLUSIVE:
			return "maxExclusive";
		case MINEXCLUSIVE:
			return "minExclusive";
		case MAXINCLUSIVE:
			return "maxInclusive";
		case MININCLUSIVE:
			return "minInclusive";
		case TOTALDIGITS:
			return "totalDigits";
		case FRACTIONDIGITS:
			return "fractionDigits";
		case LENGTH:
			return "length";
		case MINLENGTH:
			return "minLength";
		case MAXLENGTH:
			return "maxLength";
		case ENUMERATION:
			return "enumeration";
		case WHITESPACE:
			return "whiteSpace";
		case PATTERN:
			return "pattern";

		}
		return null;
	}

	/**
	 * @param facetType
	 * @return
	 */
	protected static IFacet getFacet(Facet facetType, int level,
			SchemaTag container, XmlType... typeNumber) {
		int newLevel = level + 1;
		switch (facetType) {
		case MAXEXCLUSIVE:
			return new MaxExclusive(newLevel, container, typeNumber);
		case MINEXCLUSIVE:
			return new MinExclusive(newLevel, container, typeNumber);
		case MAXINCLUSIVE:
			return new MaxInclusive(newLevel, container, typeNumber);
		case MININCLUSIVE:
			return new MinInclusive(newLevel, container, typeNumber);
		case TOTALDIGITS:
			return new TotalDigits(newLevel, container, typeNumber);
		case FRACTIONDIGITS:
			return new FractionDigits(newLevel, container, typeNumber);
		case LENGTH:
			return new Length(newLevel, container, typeNumber);
		case MINLENGTH:
			return new MinLength(newLevel, container, typeNumber);
		case MAXLENGTH:
			return new MaxLength(newLevel, container, typeNumber);
		case ENUMERATION:
			return new Enumeration(newLevel, container, typeNumber);
		case WHITESPACE:
			return new WhiteSpace(newLevel, container, typeNumber);
		case PATTERN:
			return new Pattern(newLevel, container, typeNumber);
		default:
			throw new SchemaException("Tipo di facet inesistente: " + facetType);
		}

	}

	protected static class Pattern extends SchemaTag implements IFacet {

		private static final long serialVersionUID = -497420287748053777L;

		/**
		 * @param level
		 * @param container
		 */
		Pattern(int level, SchemaTag container, XmlType... typeNumber) {
			super("pattern", level, container);
			FacetUtils.validate(Facet.PATTERN, FacetUtils.vAll, typeNumber);
		}

		public void setValue(String value) {
			addAttr("value", value);

		}

	}

	protected static class WhiteSpace extends SchemaTag implements IFacet {

		private static final long serialVersionUID = 3409996262747092162L;

		/**
		 * @param level
		 * @param container
		 */
		WhiteSpace(int level, SchemaTag container, XmlType... typeNumber) {
			super("whiteSpace", level, container);
			FacetUtils.validate(Facet.WHITESPACE, FacetUtils.vAll, typeNumber);
		}

		public void setValue(String value) {
			addAttr("value", value);
		}

	}

	protected static class MaxLength extends SchemaTag implements IFacet {

		private static final long serialVersionUID = 7257176168576865552L;

		/**
		 * @param level
		 * @param container
		 */
		MaxLength(int level, SchemaTag container, XmlType... typeNumber) {
			super("maxLength", level, container);

			FacetUtils
					.validate(Facet.MAXLENGTH, FacetUtils.vLength, typeNumber);

		}

		public void setValue(String value) {
			addAttr("value", value);
		}

	}

	protected static class MaxExclusive extends SchemaTag implements IFacet {

		private static final long serialVersionUID = 6865344213362080600L;

		/**
		 * @param level
		 * @param container
		 */
		MaxExclusive(int level, SchemaTag container, XmlType... typeNumber) {
			super("maxExclusive", level, container);

			FacetUtils.validate(Facet.MAXEXCLUSIVE, FacetUtils.vBound,
					typeNumber);

		}

		public void setValue(String value) {
			addAttr("value", value);
		}

	}

	protected static class MinExclusive extends SchemaTag implements IFacet {

		private static final long serialVersionUID = 8208886395854191064L;

		/**
		 * @param level
		 * @param container
		 */
		MinExclusive(int level, SchemaTag container, XmlType... typeNumber) {
			super("minExclusive", level, container);

			FacetUtils.validate(Facet.MINEXCLUSIVE, FacetUtils.vBound,
					typeNumber);
			;
		}

		public void setValue(String value) {
			addAttr("value", value);
		}

	}

	protected static class MaxInclusive extends SchemaTag implements IFacet {

		private static final long serialVersionUID = 7351090063941980486L;

		/**
		 * @param level
		 * @param container
		 */
		MaxInclusive(int level, SchemaTag container, XmlType... typeNumber) {
			super("maxInclusive", level, container);

			FacetUtils.validate(Facet.MAXINCLUSIVE, FacetUtils.vBound,
					typeNumber);

		}

		public void setValue(String value) {
			addAttr("value", value);
		}

	}

	protected static class MinInclusive extends SchemaTag implements IFacet {

		private static final long serialVersionUID = 6496279781068313777L;

		/**
		 * @param level
		 * @param container
		 */
		MinInclusive(int level, SchemaTag container, XmlType... typeNumber) {
			super("minInclusive", level, container);

			FacetUtils.validate(Facet.MININCLUSIVE, FacetUtils.vBound,
					typeNumber);

		}

		public void setValue(String value) {
			addAttr("value", value);
		}

	}

	protected static class TotalDigits extends SchemaTag implements IFacet {

		private static final long serialVersionUID = -4440735024800504238L;

		/**
		 * @param level
		 * @param container
		 */
		TotalDigits(int level, SchemaTag container, XmlType... typeNumber) {
			super("totalDigits", level, container);

			FacetUtils.validate(Facet.TOTALDIGITS, FacetUtils.vDigits,
					typeNumber);

		}

		public void setValue(String value) {
			addAttr("value", value);
		}

	}

	protected static class FractionDigits extends SchemaTag implements IFacet {

		private static final long serialVersionUID = 266978838129909511L;

		/**
		 * @param level
		 * @param container
		 */
		FractionDigits(int level, SchemaTag container, XmlType... typeNumber) {
			super("fractionDigits", level, container);

			FacetUtils.validate(Facet.FRACTIONDIGITS, FacetUtils.vDigits,
					typeNumber);

		}

		public void setValue(String value) {
			addAttr("value", value);
		}

	}

	protected static class Length extends SchemaTag implements IFacet {

		private static final long serialVersionUID = 3775341251291967705L;

		/**
		 * @param level
		 * @param container
		 */
		Length(int level, SchemaTag container, XmlType... typeNumber) {
			super("length", level, container);

			FacetUtils.validate(Facet.LENGTH, FacetUtils.vLength, typeNumber);

		}

		public void setValue(String value) {
			addAttr("value", value);
		}

	}

	protected static class MinLength extends SchemaTag implements IFacet {

		private static final long serialVersionUID = 4924274240487938067L;

		/**
		 * @param level
		 * @param container
		 */
		MinLength(int level, SchemaTag container, XmlType... typeNumber) {
			super("minLength", level, container);

			FacetUtils
					.validate(Facet.MINLENGTH, FacetUtils.vLength, typeNumber);

		}

		public void setValue(String value) {
			addAttr("value", value);
		}

	}

	protected static class Enumeration extends SchemaTag implements IFacet {

		private static final long serialVersionUID = -3617038469402440395L;

		/**
		 * @param level
		 * @param container
		 */
		Enumeration(int level, SchemaTag container, XmlType... types) {
			super("enumeration", level, container);
			FacetUtils.validate(Facet.ENUMERATION, FacetUtils.vEnumeration,
					types);
		}

		public void setValue(String value) {
			addAttr("value", value);
		}

	}
}
